package com.qianduo.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.qianduo.eu.AccountType;
import com.qianduo.eu.LoanType;
import com.qianduo.eu.ProductType;
import com.qianduo.eu.ProfessionType;
import com.qianduo.eu.SexType;
import com.qianduo.service.impl.CitiesServiceImpl;
import com.qianduo.vo.CitiesForm;
import com.qianduo.vo.RoleURL;

import common.util.EncodeUtil;

public class Global {
	public static ConcurrentHashMap<String,List<CitiesForm>> citiesMap	= new ConcurrentHashMap<String,List<CitiesForm>>();
	public static Map<Integer,List<RoleURL>> roleMap	= new HashMap<Integer,List<RoleURL>>();
	public static Map<String,String> accountTypeMap		= new HashMap<String,String>();
	public static Map<String,String> loanTypeMap		= new HashMap<String,String>();
	public static Map<String,String> productTypeMap		= new HashMap<String,String>();
	public static Map<String,String> professionTypeMap	= new HashMap<String,String>();
	public static Map<String,String> sexTypeMap			= new HashMap<String,String>();
	static{
		try{
			AccountType[] types1	= AccountType.values();
			for(AccountType type : types1){
				accountTypeMap.put(String.valueOf(type.getCode()), type.getName());
			}
			
			LoanType[] types2	= LoanType.values();
			for(LoanType type : types2){
				loanTypeMap.put(String.valueOf(type.getCode()), type.getName());
			}
			
			ProductType[] types3	= ProductType.values();
			for(ProductType type : types3){
				productTypeMap.put(String.valueOf(type.getCode()), type.getName());
			}
			
			ProfessionType[] types4	= ProfessionType.values();
			for(ProfessionType type : types4){
				professionTypeMap.put(String.valueOf(type.getCode()), type.getName());
			}
			
			SexType[] types5	= SexType.values();
			for(SexType type : types5){
				sexTypeMap.put(String.valueOf(type.getCode()), type.getName());
			}
		}catch(Exception e){
			Constants.log.error("Global static init enum",e);
		}
	}
	
	static{
		try{
			String path 					= EncodeUtil.decodeUTF8(Global.class.getClassLoader().getResource("").getPath());
			System.out.println(path);
			ApplicationContext ctx 			= new FileSystemXmlApplicationContext(new String[] {path + "../DatabaseContext.xml",path + "../FacadeContext.xml" });
			CitiesServiceImpl citiesService	= (CitiesServiceImpl)ctx.getBean("citiesService");
			List<CitiesForm> list			= citiesService.getAllCities();
			if(null != list && list.size() > 0){
				for(CitiesForm vo : list){
					String province			= vo.getProvince();
					List<CitiesForm> cs		= Global.citiesMap.get(province);
					if(null == cs){
						cs		= new ArrayList<CitiesForm>();
						cs.add(vo);
						Global.citiesMap.put(province, cs);
					}else{
						cs.add(vo);
					}
				}
			}
		}catch(Exception e){
			Constants.log.error("Global static init cities",e);
		}
	}
}
